<template>
  <el-form :model="registForm" :rules="fieldRules" ref="registForm" label-position="left" label-width="0px"
           class="demo-ruleForm login-container">
    <span class="tool-bar">
    </span>
    <h2 class="title">注册</h2>
    <el-form-item prop="account">
      <el-input type="text" v-model="registForm.account" auto-complete="off" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item prop="email">
      <el-input type="email" v-model="registForm.email" auto-complete="off" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password" v-model="registForm.password" auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item prop="repassword">
      <el-input type="password" v-model="registForm.repassword" auto-complete="off" placeholder="确认密码"></el-input>
    </el-form-item>

    <el-form-item style="width:100%;">
      <el-button type="primary" style="width:47.5%;" @click="$router.push('/loginm')">已有账号？</el-button>
      <el-button type="primary" style="width:47.5%;" @click.native.prevent="register" :loading="loading">注 册</el-button>

    </el-form-item>
  </el-form>
</template>

<script>
  import Cookies from "js-cookie";

  export default {
    name: 'Login',
    data() {
      return {
        loading: false,
        registForm: {
          account: '',
          email: '',
          password: '',
          repassword: ''
        },
        fieldRules: {
          account: [  // 字母+数字
            {required: true, message: '请输入账号', trigger: 'blur'},
            {pattern: /^[A-Za-z0-9]{1,16}$/, message: '16个以内字母或数字', trigger: ['blur', 'change']}
          ],
          password: [ // 六到十六位
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min:6, max:16 , message: '6~16位字符', trigger: ['blur', 'change']}
          ],
          repassword: [
            {required: true, message: '请再次输入密码', trigger: 'blur'},
            {validator: this.valireassword, trigger: ['blur', 'change']}
          ],
          email: [
            {required: true, message: '请输入邮箱', trigger: 'blur'},
            {type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']}
          ]
        }
      }
    },
    methods: {
      register() {
        this.loading = true
        this.$refs["registForm"].validate((valid) => {
            if (valid) {
              let registInfo = {
                username: this.registForm.account,
                password: this.registForm.password,
                email: this.registForm.email
              }
              this.$api.user.doregister(registInfo).then((res) => {
                if (res.msg === 'success') {
                  this.$message({message: '注册成功！请登录', type: 'success'})
                  this.$router.push('/loginm')
                } else {
                  this.$message({message: res.msg, type: 'error'})
                }
              }).catch((res) => {
                this.$message({message: res.message, type: 'error'})
              })
            } else {
              console.log('error submit!!');
              this.$message({message: '请按检查输入格式', type: 'warning'})
              this.loading = false
              return false
            }
          }
        );
        this.loading = false
      },
      valireassword(rule, value, callback) {
        if (value !== this.registForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      },
      reset() {
        this.$refs.registForm.resetFields()
      }
    },
    mounted() {
      Cookies.set('token', null) // 放置token到Cookie
      Cookies.set('role', 'annoym')
    }
  }
</script>

<style lang="scss" scoped>
  .login-container {
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 100px auto;
    width: 300px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;

    .title {
      margin: 0px auto 30px auto;
      text-align: center;
      color: #505458;
    }
  }
</style>
